import Vue from 'vue'
import VueRouter from 'vue-router'
const changePwd = () => import('../views/changePwd')
const site = () => import('../views/SiteDistribution')
const realData = () => import('../views/realData')
const dataAnalysis = () => import('../views/dataAnalysis')
const contrastiveAnalysis = () => import('../views/contrastiveAnalysis')
const riskAnalysis = () => import('../views/riskAnalysis')
const dataStatistics = () => import('../views/dataStatistics')
const warningManagement = () => import('../views/warningManagement')
const waringLog = () => import('../views/waringLog')
const systemManagement = () => import('../views/systemManagement')
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [{
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue')
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/index.vue')
  },
  {
    path: '/operatePage',
    name: 'operatePage',
    component: () => import('../views/operatePage/index.vue'),
    children: [{
        path: '/changePwd',
        component: changePwd,
      },
      {
        path: '/site',
        component: site
      },
      {
        path: '/realData',
        component: realData,

      },
      {
        path: '/dataAnalysis',
        component: dataAnalysis
      },
      {
        path: '/contrastiveAnalysis',
        component: contrastiveAnalysis
      },
      {
        path: '/riskAnalysis',
        component: riskAnalysis
      },
      {
        path: '/dataStatistics',
        component: dataStatistics
      },
      {
        path: '/warningManagement',
        component: warningManagement
      },
      {
        path: '/waringLog',
        component: waringLog
      },
      {
        path: '/systemManagement',
        redirect: '/systemManagement/deviceCollection',
        component: systemManagement,
        children: [{
            path: 'deviceCollection',
            component: () => import('../views/systemManagement/deviceCollection.vue'),
          },
          {
            path: 'warningThreshold',
            component: () => import('../views/systemManagement/warningThreshold.vue')
          },
          {
            path: 'warningNotice',
            component: () => import('../views/systemManagement/warningNotice.vue')
          },
          {
            path: 'monitor',
            component: () => import('../views/systemManagement/monitor.vue')
          },
          {
            path: 'referenceSystem',
            component: () => import('../views/systemManagement/referenceSystem.vue')
          },
          {
            path: 'statisticalReport',
            component: () => import('../views/systemManagement/statisticalReport.vue')
          },
          {
            path: 'dataDownload',
            component: () => import('../views/systemManagement/dataDownload.vue')
          },
          {
            path: 'userManagement',
            component: () => import('../views/systemManagement/userManagement.vue')
          },
          {
            path: 'roleManagement',
            component: () => import('../views/systemManagement/roleManagement.vue')
          },
          {
            path: 'authorityManagement',
            component: () => import('../views/systemManagement/authorityManagement.vue')
          },
          {
            path: 'logManagement',
            component: () => import('../views/systemManagement/logManagement.vue')
          },
          {
            path: 'versionUpdate',
            component: () => import('../views/systemManagement/versionUpdate.vue')
          },
          {
            path: 'instructions',
            component: () => import('../views/systemManagement/instructions.vue')
          },
        ]
      },
    ]
  },
  {
    path: '*',
    redirect: '/login'
  },
]

const router = new VueRouter({
  mode: "history",
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return {
        selector: to.hash
      }
    }
  }
})

router.beforeEach((to, from, next) => {
  let isLogin = window.sessionStorage.getItem("token");
  if (isLogin) {
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
})

export default router